using System.Collections.Generic;

namespace Developex.DatabaseWrapper.CSVImportExport
{

	///<summary>
	///</summary>
	public class EIProperties
	{
		/// <summary>
		/// 
		/// </summary>
		protected List<EIField> excludeFieldsList = new List<EIField>();
		/// <summary>
		/// 
		/// </summary>
		protected List<EIField> addedFictitiousFieldsList = new List<EIField>();
		/// <summary>
		/// 
		/// </summary>
		protected List<EIField> defaultFieldsList = new List<EIField>();
		/// <summary>
		/// 
		/// </summary>
		protected List<EIField> connectedFieldsList = new List<EIField>();
		/// <summary>
		/// 
		/// </summary>
		protected List<EICalculatedField> calculatedFieldsList = new List<EICalculatedField>();

		///<summary>
		///</summary>
		///<param name="connectedField"></param>
		public void AddConnectedField(EIField connectedField)
		{
			this.connectedFieldsList.Add(connectedField);
		}

		private static EIField GetField(IEnumerable<EIField> fieldList, string name)
		{
			foreach (EIField field in fieldList)
			{
				if (field.Name == name)
				{
					return field;
				}
			}
			return null;
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<returns></returns>
		public EIField GetDefaultField(string name)
		{
			return GetField(this.defaultFieldsList, name);
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<returns></returns>
		public EIField GetConnectedFieldByDisplayName(string name)
		{
			foreach (EIField field in this.connectedFieldsList)
			{
				if (field.DisplayName == name)
				{
					return field;
				}
			}
			return null;
		}

		///<summary>
		///</summary>
		public List<EICalculatedField> CalculatedFieldList
		{
			get { return calculatedFieldsList; }
		}

		///<summary>
		///</summary>
		///<param name="field"></param>
		public void AddCalculatedField(EICalculatedField field)
		{
			calculatedFieldsList.Add(field);
		}

		///<summary>
		///</summary>
		///<param name="field"></param>
		///<param name="calculateDelegate"></param>
		public void AddCalculatedField(EICalculatedField field, EICalculatedField.GetValueOfCalculatedField calculateDelegate)
		{
			if (field != null)
			{
				field.GetValue += calculateDelegate;
				calculatedFieldsList.Add(field);
			}
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		///<param name="displayName"></param>
		///<param name="position"></param>
		///<param name="calculateDelegate"></param>
		public void AddCalculatedField(string name, object value, string displayName, int? position, EICalculatedField.GetValueOfCalculatedField calculateDelegate)
		{
			EICalculatedField field = new EICalculatedField(name, value, displayName, position);
			field.GetValue += calculateDelegate;
			calculatedFieldsList.Add(field);
		}

		///<summary>
		///</summary>
		public List<EIField> AddedFictitiousFieldsList
		{
			get { return addedFictitiousFieldsList; }
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		public void AddFictitiousFields(string name)
		{
			this.addedFictitiousFieldsList.Add(new EIField(name));
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="displayName"></param>
		public void AddFictitiousFields(string name, string displayName)
		{
			this.addedFictitiousFieldsList.Add(new EIField(name, null, displayName));
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="displayName"></param>
		///<param name="position"></param>
		public void AddFictitiousFields(string name, string displayName, int? position)
		{
			this.addedFictitiousFieldsList.Add(new EIField(name, null, displayName, position));
		}

		///<summary>
		///</summary>
		public List<EIField> ExcludeFieldsList
		{
			get { return excludeFieldsList; }
		}

		///<summary>
		///</summary>
		public List<EIField> DefaultFieldsList
		{
			get { return defaultFieldsList; }
		}

		///<summary>
		///</summary>
		public void Clear()
		{
			this.defaultFieldsList.Clear();
			this.excludeFieldsList.Clear();
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		public void AddExludeField(string name)
		{
			this.excludeFieldsList.Add(new EIField(name));
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		public void AddDefaultField(string name, object value)
		{
			this.defaultFieldsList.Add(new EIField(name, value));
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		///<param name="position"></param>
		public void AddDefaultField(string name, object value, int? position)
		{
			this.defaultFieldsList.Add(new EIField(name, value, name, position));
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<returns></returns>
		public bool ExistDefaultField(string name)
		{
			foreach (EIField field in defaultFieldsList)
			{
				if (field.Name == name)
					return true;
			}
			return false;
		}


		///<summary>
		///</summary>
		///<param name="name"></param>
		///<returns></returns>
		public bool ExistExcludeField(string name)
		{
			foreach (EIField field in excludeFieldsList)
			{
				if (field.Name == name)
					return true;
			}
			return false;
		}
	}
}